#ifndef __WIZ_MUTEX_INNER_LINUX_HPP__SHANHAOBO_19800429__
#define __WIZ_MUTEX_INNER_LINUX_HPP__SHANHAOBO_19800429__

#include <pthread.h>
#include "../wiz_mutex_inner.hpp"

namespace Wiz
{
    namespace Inner
    {
        namespace Mutex
        {
            namespace linux
            {
                class mutex_t : public ::Wiz::Inner::Mutex::mutex_t
                {
                protected:
                    pthread_mutex_t m_Mutex;
                public:
                    mutex_t()
                    {
                        ::Wiz::Int::Type r = pthread_mutex_init(&m_Mutex, 0);
                        WIZ_ASSERT(r == 0);
                    }
                    ~mutex_t()
                    {
                        ::Wiz::Int::Type r = pthread_mutex_destroy(&m_Mutex);
                        WIZ_ASSERT(r == 0);
                    }
                public:
                    virtual ::Wiz::Bool::Type TimeAcquire(::Wiz::UInt32::In t)
                    {
                        ::Wiz::CountDown::Type cd;
                        cd.start(t);
                        while (cd.timeover())
                        {
                            ::Sleep(1);
                            if (this->TryAcquire())
                            {
                                return ::Wiz::Bool::True;
                            }
                        }
                        return ::Wiz::Bool::False;
                    }
                    virtual ::Wiz::Void::Type Acquire()
                    {
                        ::Wiz::Int::Type r = pthread_mutex_Lock(&m_Mutex);
                        WIZ_ASSERT(r == 0);
                    }
                    virtual ::Wiz::Bool::Type TryAcquire()
                    {
                        ::Wiz::Int::Type r = pthread_mutex_tryLock(&m_Mutex);
                        return r == 0;
                    }
                    virtual ::Wiz::Void::Type Release()
                    {
                        ::Wiz::Int::Type r = pthread_mutex_unLock(&m_Mutex);
                        WIZ_ASSERT(r == 0);
                    }
                };
            } /// end of namespace windows
        } /// end of namespace Mutex
    } /// end of namespace Inner
} /// end of namespace Wiz

#endif /*__WIZ_MUTEX_INNER_LINUX_HPP__SHANHAOBO_19800429__*/
